package com.yg.edu.lock;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.locks.LockSupport;

/**
 * @author ：杨过
 * @date ：Created in 2020/4/28
 * @version: V1.0
 * @slogan: 天下风云出我辈，一入代码岁月催
 * @description:
 **/
@Slf4j
public class Juc01_Thread_LockSupport {

    /**
     * 逻辑：park住，解锁park，中断线程
     * 输出内容：
     * 17:44:28.179 [t0] INFO com.yg.edu.lock.Juc01_Thread_LockSupport - t0,开始执行!
     * 17:44:28.184 [t0] INFO com.yg.edu.lock.Juc01_Thread_LockSupport - 准备park住当前线程：t0....
     * 17:44:33.176 [main] INFO com.yg.edu.lock.Juc01_Thread_LockSupport - 准备唤醒t0线程!
     * 17:44:33.177 [t0] INFO com.yg.edu.lock.Juc01_Thread_LockSupport - 线程状态是：false
     * 17:44:33.177 [t0] INFO com.yg.edu.lock.Juc01_Thread_LockSupport - 当前线程t0已经被唤醒....
     * 17:44:33.177 [t0] INFO com.yg.edu.lock.Juc01_Thread_LockSupport - 准备park住当前线程：t0....
     * 17:44:35.182 [t0] INFO com.yg.edu.lock.Juc01_Thread_LockSupport - 线程状态是：true
     * 17:44:35.183 [t0] INFO com.yg.edu.lock.Juc01_Thread_LockSupport - 当前线程t0已经被唤醒....
     * 17:44:35.183 [t0] INFO com.yg.edu.lock.Juc01_Thread_LockSupport - 准备park住当前线程：t0....
     * @param args
     * @return void
     * @author fwh [2022/2/26 && 5:45 下午]
     */
    public static void main(String[] args) {
        Thread t0 = new Thread(new Runnable() {
            @Override
            public void run() {
                Thread current = Thread.currentThread();
                log.info("{},开始执行!",current.getName());
                for(;;){//spin 自旋
                    log.info("准备park住当前线程：{}....",current.getName());
                    LockSupport.park();
//                    System.out.println("线程状态是："+Thread.interrupted());
                    log.info("线程状态是："+Thread.interrupted());
                    log.info("当前线程{}已经被唤醒....",current.getName());

                }
            }
        },"t0");

        t0.start();

        try {
            Thread.sleep(5000);
            log.info("准备唤醒{}线程!",t0.getName());
            LockSupport.unpark(t0);
            Thread.sleep(2000);
            t0.interrupt();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
